layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    var tableIns = table.render({
        elem: '#test'
        , url: '/api/project/index'
        , toolbar: '#toolbarDemo'
        , title: '分类',
        limit: 20,
        limits: [10, 15, 20, 25],
        id: "newsListTable"
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
            , {field: 'title', title: '标题', width: 150, edit: 'text'},
            {
                field: 'pic', title: '缩略图', width: 120, align: 'center', templet: function (d) {
                    return '<img src="' + d.pic + '" >'
                }
            }
            , {field: 'url', title: '链接', width: 150, edit: 'text'}
            , {field: 'type', title: '类型', width: 150, edit: 'text'}
            , {field: 'created_at', title: '创建时间'}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
        ]]
        , page: true
    });
    //搜索
    $(".search_btn").on("click", function () {
        serialize = $('.layui-form').serializeArray();
        if ($(".searchVal").val() != '') {
            table.reload("newsListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: serialize  //搜索的关键字
                }
            })
        } else {
            winui.window.msg("请输入搜索的内容");
        }
        return false;
    });
    //批量删除
    $(".delAll_btn").click(function (e) {
        var self = this;
        winui.window.confirm('真的删除么', function (index) {
            layer.close(index);
            var checkStatus = table.checkStatus("newsListTable");
            var data = checkStatus.data;
            var ids = [];
            $.each(data, function (i, item) {
                ids[i] = item.id;
            })
            if (ids.length > 0) {
                data = {
                    'ids': ids
                }
                var index = '';
                $.ajax({
                    type: "post",
                    url: "/admin/project/del",
                    data: data,
                    beforeSend: function () {
                        index = layer.load(1, {
                            shade: [0.1, '#fff'] //0.1透明度的白色背景
                        });
                        $(self).attr('disabled', "disabled");
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            winui.window.msg('删除成功')
                            setTimeout(function () {
                                tableIns.reload();
                            }, 250)
                        } else {
                            winui.window.msg('删除失败');
                        }
                    },
                    complete: function () {
                        $(self).removeAttr("disabled");
                        layer.close(index);
                    }
                })
            }
        });
        return false;

    });
    //添加
    $(".addNews_btn").click(function (e) {
        addNews();
        return false;
    })
    $(".reload_btn").click(function () {
        tableIns.reload();
        return false;
    })
    var lock = false;

    //添加文章
    function addNews(edit = null) {
        self = this;
        if (!edit) {
            var edit = {
                'id': ''
            };
        }
        winui.window.open({
            id: '公告',
            type: 2,
            title: '添加开源项目',
            area: ['70vw', '80vh'],
            content: "/admin/project/add?id=" + edit.id,
            success: function (layero, index) {
                var obj = layero.find("iframe")[0].contentWindow;
                if (edit.id) {
                    console.log(edit)
                    obj.$('#id').val(edit.id);
                    obj.$('#title').val(edit.title);
                    obj.$('input[id=type][value=' + edit.type + ']').attr('checked', 'checked');
                    obj.$('#url').val(edit.url);
                    obj.$('#sort').val(edit.sort);
                    obj.$('#pic').val(edit.pic);
                    obj.$('#srcimgurl').attr('src',edit.pic);
                }
                obj.layui.form.render();
                obj.layui.form.on("submit(addNews)", function (data) {
                    data.field['id'] = edit.id;
                    if (lock == true) {
                        return false;
                    }
                    lock = true;
                    $.post("/admin/project/store", data.field, function (data1) {
                        winui.window.msg(data1.msg);
                        if (data1.code == 200) {
                            layero.find('.layui-layer-ico.layui-layer-close').trigger('click');
                            layer.close(index);
                            setTimeout(function () {
                                tableIns.reload();
                                lock = false;
                            }, 1000)
                        }
                    })
                    lock = false;
                });
                // obj.$('.layui-btn').click(function(){
                //     layer.close(index);
                //     layero.find('.layui-layer-ico.layui-layer-close').trigger('click');
                //     setTimeout(function(){
                //         tableIns.reload();
                //     },250)
                // })
                return false;
            }
        });
        return false;
    }

    //头工具栏事件
    table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'getCheckData':
                var data = checkStatus.data;
                winui.window.alert(JSON.stringify(data));
                break;
            case 'getCheckLength':
                var data = checkStatus.data;
                winui.window.msg('选中了：' + data.length + ' 个');
                break;
            case 'isAll':
                winui.window.msg(checkStatus.isAll ? '全选' : '未全选');
                break;
        }
        ;
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            data = {'ids': data.id};
            winui.window.confirm('确定删除此？', {icon: 3, title: '提示信息'}, function (index) {
                winui.window.post("/admin/project/del", data, function (data) {
                    winui.window.msg(data.msg);
                    tableIns.reload();
                })
            })
        } else if (obj.event === 'edit') {
            addNews(data);
        }
    });

});
